﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;

namespace AdminUI.Pages.BaseData
{
    public partial class UserEditModel : BaseModel
    {

        public void OnGet(long id)
        {
            Current = UserBO.Instance.GetItem<User>(id);

            var recoName = string.Empty;
            if(Current.RecoID > 0)recoName  = $"{Current.RecoID}-{Current.RecoName}";
            ViewBag.RecoName = recoName;

            var agentCity = string.Empty;
            if(Current.AgentAreaID > 0)
            {
                var city = AreaBO.Instance.GetCity(Current.AgentAreaID);
                agentCity = $"{city.AreaID}-{city.AreaName}";
            }
            ViewBag.AgentCity = agentCity;

            var countyName = string.Empty;
            if (Current.CountyID > 0) countyName = $"{Current.CountyID}-{Current.CountyName}";
            ViewBag.CountyName = countyName;
        }

        public User Current { get; set; }

        public IActionResult OnPostBtnSubmit_Click(UserModifyDto dto)
        {
            try
            {
                UserBO.Instance.Modify(dto);

                var js = ActiveWindow.GetHideExecuteScriptReference("onRefreshClick();");
                FineUICore.PageContext.RegisterStartupScript(js); //保存后触发父页面刷新按钮事件

                //FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }

    }
}